<template>
	<view class="bookshelf-container">
		<!-- 顶部操作栏 -->
		<view class="operation-bar">
			<view class="edit-btn" @tap="toggleEdit">{{ isEdit ? '完成' : '编辑' }}</view>
		</view>
		
		<!-- 书籍列表 -->
		<scroll-view scroll-y class="book-list">
			<view class="book-item" v-for="(book, index) in bookList" :key="index" @tap="handleBookTap(book)">
				<view class="checkbox" v-if="isEdit" @tap.stop="toggleSelect(index)">
					<view :class="['checkbox-inner', { checked: selectedBooks.includes(index) }]"></view>
				</view>
				<image class="book-cover" :src="book.cover" mode="aspectFill"></image>
				<view class="book-info">
					<text class="book-name">{{ book.name }}</text>
					<text class="read-progress">已读 {{ book.progress }}%</text>
					<text class="update-info" v-if="book.isUpdate">更新{{ book.updateCount }}章</text>
				</view>
			</view>
		</scroll-view>
		
		<!-- 底部操作栏 -->
		<view class="bottom-bar" v-if="isEdit">
			<view class="select-all" @tap="selectAll">
				<view :class="['checkbox-inner', { checked: isAllSelected }]"></view>
				<text>全选</text>
			</view>
			<view class="delete-btn" @tap="deleteBooks">删除</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isEdit: false,
			selectedBooks: [],
			bookList: [
				{
					id: 1,
					name: '三体',
					cover: '/static/875f-kpamyih7048893.jpg',
					progress: 35,
					isUpdate: true,
					updateCount: 3
				},
				{
					id: 2,
					name: '活着',
					cover: '/static/01d7a06376f6a0000ed21000391d95.jpg',
					progress: 68,
					isUpdate: false
				},
				// 更多书籍数据...
			]
		}
	},
	computed: {
		isAllSelected() {
			return this.selectedBooks.length === this.bookList.length
		}
	},
	methods: {
		toggleEdit() {
			this.isEdit = !this.isEdit
			if (!this.isEdit) {
				this.selectedBooks = []
			}
		},
		handleBookTap(book) {
			if (!this.isEdit) {
				uni.navigateTo({
					url: `/pages/reader/reader?id=${book.id}`
				})
			}
		},
		toggleSelect(index) {
			const selectedIndex = this.selectedBooks.indexOf(index)
			if (selectedIndex > -1) {
				this.selectedBooks.splice(selectedIndex, 1)
			} else {
				this.selectedBooks.push(index)
			}
		},
		selectAll() {
			if (this.isAllSelected) {
				this.selectedBooks = []
			} else {
				this.selectedBooks = this.bookList.map((_, index) => index)
			}
		},
		deleteBooks() {
			uni.showModal({
				title: '提示',
				content: '确定删除选中的书籍吗？',
				success: (res) => {
					if (res.confirm) {
						this.bookList = this.bookList.filter((_, index) => !this.selectedBooks.includes(index))
						this.selectedBooks = []
						this.isEdit = false
					}
				}
			})
		}
	}
}
</script>

<style lang="scss">
.bookshelf-container {
	min-height: 100vh;
	background: #f5f5f5;
	padding-bottom: 100rpx;
	
	.operation-bar {
		height: 88rpx;
		background: #fff;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding: 0 30rpx;
		
		.edit-btn {
			font-size: 28rpx;
			color: #666;
		}
	}
	
	.book-list {
		padding: 20rpx;
		
		.book-item {
			display: flex;
			align-items: center;
			background: #fff;
			padding: 20rpx;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			
			.checkbox {
				margin-right: 20rpx;
				
				.checkbox-inner {
					width: 40rpx;
					height: 40rpx;
					border: 2rpx solid #ddd;
					border-radius: 50%;
					
					&.checked {
						background: #3cc51f;
						border-color: #3cc51f;
					}
				}
			}
			
			.book-cover {
				width: 160rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
			
			.book-info {
				flex: 1;
				margin-left: 20rpx;
				
				.book-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}
				
				.read-progress {
					font-size: 24rpx;
					color: #999;
					margin: 10rpx 0;
					display: block;
				}
				
				.update-info {
					font-size: 24rpx;
					color: #3cc51f;
				}
			}
		}
	}
	
	.bottom-bar {
		color: #333;
		
		.select-all {
			display: flex;
			align-items: center;
			
			.checkbox-inner {
				width: 40rpx;
				height: 40rpx;
				border: 2rpx solid #ddd;
				border-radius: 50%;
				margin-right: 20rpx;
				
				&.checked {
					background: #3cc51f;
					border-color: #3cc51f;
				}
			}
			
			text {
				font-size: 28rpx;
				color: #333;
			}
		}
		
		.delete-btn {
			width: 160rpx;
			height: 60rpx;
			
			font-size: 28rpx;
			border-radius: 30rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
}
</style> 